package com.woniu.zsd.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.order.dto.FindOrderInfoDto;
import com.woniu.zsd.order.entity.OrderInfo;
import com.woniu.zsd.order.entity.OrderWaste;
import com.woniu.zsd.order.from.WorkUpdateWasteFrom;
import com.woniu.zsd.order.mapper.OrderInfoMapper;
import com.woniu.zsd.order.mapper.OrderWasteMapper;
import com.woniu.zsd.order.service.IOrderWasteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class OrderWasteServiceImpl extends ServiceImpl<OrderWasteMapper, OrderWaste> implements IOrderWasteService {

    @Resource
    private OrderWasteMapper orderWasteMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Transactional
    @Override
    public void workUpdateWaste(List<WorkUpdateWasteFrom> wwf,Integer id) throws Exception {
        if (wwf==null||wwf.isEmpty()) throw new Exception("请工作人员补充确认废品信息");

        List<OrderWaste> orderWastes = new ArrayList<>();
        BigDecimal bigDecimal = BigDecimal.valueOf(0);
        String orderNum = wwf.get(0).getOrderNum();
        OrderInfo order = orderInfoMapper.findOrderByNumber(orderNum);
        if (order.getOrderState()!=3) throw new Exception("订单状态异常");
        for (WorkUpdateWasteFrom workUpdateWasteFrom : wwf) {

            OrderWaste orderWaste = new OrderWaste();
            orderWaste.setOrderNum(workUpdateWasteFrom.getOrderNum());
            orderWaste.setWasteInfo(workUpdateWasteFrom.getWasteInfo());
            orderWaste.setSasteTypeId(workUpdateWasteFrom.getSasteTypeId());
            orderWaste.setSastePrice(workUpdateWasteFrom.getSastePrice());
            //获取单个废品的总价
            BigDecimal multiply = workUpdateWasteFrom.getSastePrice().multiply(BigDecimal.valueOf(workUpdateWasteFrom.getWeight()));
            orderWaste.setSasteAllPrice(multiply);
            //将废品总价累加获得订单总价
            bigDecimal = bigDecimal.add(multiply);
            orderWaste.setUnit(workUpdateWasteFrom.getUnit());
            orderWaste.setWasteImage(workUpdateWasteFrom.getWasteImage());
            orderWaste.setWeight(workUpdateWasteFrom.getWeight());
            orderWastes.add(orderWaste);
        }
        orderWasteMapper.addOrderWaste(orderWastes);
        List<OrderWaste> orderWasteByOrderNumber = orderWasteMapper.findOrderWasteByOrderNumber(orderNum);
        BigDecimal totalPrice = BigDecimal.ZERO;
        // 遍历集合并累加价格
        for (OrderWaste orderWaste : orderWasteByOrderNumber) {
            BigDecimal sasteAllPrice = orderWaste.getSasteAllPrice();
            if (sasteAllPrice != null) { // 防止空指针异常
                totalPrice = totalPrice.add(sasteAllPrice);
            }
        }

        orderInfoMapper.upAllPrice(totalPrice,orderNum);
    }

    @Override
    public List<FindOrderInfoDto> findOrderInfo(String orderNumber) {
        List<OrderWaste> orderWaste = orderWasteMapper.findOrderWasteByOrderNumber(orderNumber);
        // 2. 检查是否查询到数据
        if (orderWaste == null || orderWaste.isEmpty()) {
            return Collections.emptyList(); // 返回空列表，避免返回 null
        }

        // 3. 使用工具类将 OrderWaste 转换为 FindOrderInfoDto
        List<FindOrderInfoDto> result = new ArrayList<>();
        for (OrderWaste orderWastes : orderWaste) {
            FindOrderInfoDto dto = BeanUtil.toBean(orderWastes, FindOrderInfoDto.class);
            result.add(dto);
        }

        // 4. 返回转换后的结果
        return result;
    }
}
